package dreamCookGame;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public class ItemMenu {
	BufferedImage _img;
	int _x;
	int _y;
	String[] _menuOptions;
	Button[] _itemMenuButtons;

	public ItemMenu(int x, int y, String[] menuOptions) {
		_x = x-67;
		_y = y-53;
		ensureOnScreen();
		_menuOptions = menuOptions;
		_itemMenuButtons = new Button[_menuOptions.length];
		createMenuButtons();
	}
	
	public void loadResources(References reference) {
		_img = (reference.getImageArray("itemMenuCenter"))[0];

		for(int i = 0; i < _menuOptions.length; i++)
			_itemMenuButtons[i].loadResources(reference);
	}
	
	private void ensureOnScreen() {
		
		//magic placement numbers
		int leftBound = 50;
		int rightBound = 615;
		int topBound = 90;
		int bottomBound = 493;
		
		if(_x<leftBound){
			_x = leftBound;
		}
		else if(_x>rightBound){
			_x = rightBound;
		}
		if(_y<topBound){
			_y = topBound;
		}
		else if(_y>bottomBound){
			_y = bottomBound;
		}
	}

	private void createMenuButtons() {
		for( int i = 0; i<_menuOptions.length; i++ ) {
			
			_itemMenuButtons[i] = buildButton(i,_menuOptions[i]);
			
			/*  //This is silly
			    //     |
			    //     |
			    //     V
			if(_menuOptions[i] == "addToInventory"){
				//TODO: Make rectangle size dependent on button image (Brittany)
				_itemMenuButtons[i] = buildButton(i,"addToInventory");
			}
			else if(_menuOptions[i] == "visit"){
				//TODO: Make rectangle size dependent on button image (Brittany)
				_itemMenuButtons[i] = buildButton(i,"visit");
			}
			else if(_menuOptions[i] == "visitAndProgress"){
				//TODO: Make rectangle size dependent on button image (Brittany)
				_itemMenuButtons[i] = buildButton(i,"visitAndProgress");
			}
			else if(_menuOptions[i] == "talkTo"){
				//TODO: Make rectangle size dependent on button image (Brittany)
				//TODO: use "talkTo" image, rather than "addToInventory" image (Jonathan)
				_itemMenuButtons[i] = buildButton(i,"talkTo");
			}
			else if(_menuOptions[i] == "taste"){
				//TODO: Make rectangle size dependent on button image (Brittany)
				//TODO: use "taste" image, rather than "addToInventory" image (Jonathan)
				_itemMenuButtons[i] = buildButton(i,"taste");
			}
			else if(_menuOptions[i] == "mix"){
				//TODO: Make rectangle size dependent on button image (Brittany)
				//TODO: use "mix" image, rather than "addToInventory" image (Jonathan)
				_itemMenuButtons[i] = buildButton(i,"mix");
			}
			else if(_menuOptions[i] == "addToBowl"){
				//TODO: Make rectangle size dependent on button image (Brittany)
				//TODO: use "addToBowl" image, rather than "addToInventory" image (Jonathan)
				_itemMenuButtons[i] = buildButton(i,"addToBowl");
			}
			else if(_menuOptions[i] == "nullOption"){
				//TODO: Make rectangle size dependent on button image (Brittany)
				//TODO: use "addToBowl" image, rather than "addToInventory" image (Jonathan)
				_itemMenuButtons[i] = buildButton(i,"nullOption");
			}
			
			*/
		}
	}

	private Button buildButton(int position, String imgNames) {
		float radius = 75;
		int shiftX = 30;
		int shiftY = -20;
		int imageSize = 78;
		int calculatedX = (int)(_x+shiftX-(radius*Math.cos(((double)position*Math.PI)/3)));
		int calculatedY = (int)(_y+shiftY-(radius*Math.sin(((double)position*Math.PI)/3)));
		Rectangle boundingBox = new Rectangle(calculatedX+14, calculatedY+14, imageSize-28, imageSize-28);
		String[] stateImgNames = {imgNames};
		Button button = new Button(calculatedX, calculatedY, boundingBox, stateImgNames);
		return button;
	}

	public void shift(int dx, int dy) {
		_x += dx;
		_y += dy;
		ensureOnScreen();
		if(_itemMenuButtons!=null){
			for(int i=0; i<_itemMenuButtons.length;i++){
				float radius = 75;
				int shiftX = 30;
				int shiftY = -20;
				int imageSize = 78;
				int calculatedX = (int)(_x+shiftX-(radius*Math.cos(((double)i*Math.PI)/3)));
				int calculatedY = (int)(_y+shiftY-(radius*Math.sin(((double)i*Math.PI)/3)));
				_itemMenuButtons[i].updatePositionRectangle(calculatedX, calculatedY);
				Rectangle boundingBox = new Rectangle(calculatedX+14, calculatedY+14, imageSize-28, imageSize-28);
				_itemMenuButtons[i]._boundingBox = boundingBox;
			}
			
		}
	}
	 

}
